package com.youxin.foundation.utils.thread;

import com.youxin.foundation.utils.locks.LockObject;

/**
 * 全局单任务线程池
 * @author yingde.cao
 *
 */
public class SingleThreadPool extends BaseThreadPool{

	public SingleThreadPool() {
		super(1, 1, 5000);
	}
	
	private static SingleThreadPool threadPool;
	/**
	 * 单例
	 * 
	 * @param corePoolSize
	 *            线程池核心运行数
	 * @param maxPoolSize
	 *            最大线程池
	 * @param keepAliveTime
	 *            保留时间
	 * @return
	 */
	public static SingleThreadPool instance() {
		if (threadPool == null) {
			LockObject.lock(() -> {
				if (threadPool != null) {
					return;
				}
				threadPool = new SingleThreadPool();
			});
		}
		return threadPool;
	}

	
	/**
	 * 添加运行任务
	 * 
	 * @param runnable 运行任务
	 */
	public static void addTask(Runnable runnable) {
		if (threadPool == null) {
			instance();
		}
		threadPool.getExecutorService().execute(runnable);
	}
	
	
	
}

